/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.PhuTrach_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class PhuTrach_DAO {
    
    public static String Get_IDPT() throws SQLException{
        String IDPT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "PT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            IDPT = Rs.getString(1);
        }
        return IDPT;
    }
    
    public static ArrayList<PhuTrach_DTO> GetALL()
    {
        ArrayList<PhuTrach_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from PhuTrach" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                PhuTrach_DTO PhuTrach = new PhuTrach_DTO();
                PhuTrach.setID_PT(rs.getString("ID_PT"));
                PhuTrach.setMaNV(rs.getString("MaNV"));
                PhuTrach.setMaCT(rs.getString("MaCT"));
                PhuTrach.setMaToaTau(rs.getString("MaToa"));
                PhuTrach.setNgayDi(rs.getDate("NgayDi"));
                ds.add(PhuTrach);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(PhuTrach_DTO PhuTrach)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayDi = fmt.format(PhuTrach.getNgayDi());
            String sql = "Insert PhuTrach Values('"+PhuTrach.getID_PT()+"','"+PhuTrach.getMaNV()+"','"+PhuTrach.getMaCT()+"','"+PhuTrach.getMaToaTau()+"','"+NgayDi+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(PhuTrach_DTO PhuTrach)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayDi = fmt.format(PhuTrach.getNgayDi());
            String sql = "Update PhuTrach Set MaNV = '"+ PhuTrach.getMaNV() +"', MaCT = '"+PhuTrach.getMaCT()+"', MaToaTau = '"+PhuTrach.getMaToaTau()+"', NgayDi = '"+NgayDi+ "' where ID_PT = '"+ PhuTrach.getID_PT()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String ID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from PhuTrach where ID_PT = '"+ID+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
